package com.xinghou.platform.server.api.bussiness.impl;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;
import com.xinghou.platform.entity.req.MsgInfoReq;
import com.xinghou.platform.server.api.bussiness.JiGuangPushService;
import com.xinghou.platform.server.api.bussiness.MsgInfoBusinessService;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 * Created by qingshengzheng on 16/6/28.
 */
@Service("jiguangPushService")
public class JiGuangPushServiceImpl implements JiGuangPushService {
    private String JGappKey = ConfigUtil.getProperty("JGappKey");
    private String JGappSecret = ConfigUtil.getProperty("JGappSecret");
    private JPushClient jpushClient =new JPushClient(JGappSecret, JGappKey, 3);

    @Resource
    private MsgInfoBusinessService msgInfoBusinessService;
    @Override
    public void sendUserMessage(MsgInfoReq infoReq) {
        msgInfoBusinessService.sendNewMsg(infoReq);
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(infoReq.getTouserid()))
                .setNotification(Notification.alert(infoReq.getContent()))
                .build();
        pushMessage(pushPayload);
    }


    public String pushMessage(PushPayload pushPayload) {
        try {
            PushResult result = jpushClient.sendPush(pushPayload);
            return "";
        } catch (APIConnectionException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        } catch (APIRequestException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        }
    }

    public String pushScheduleMessage(String time,PushPayload pushPayload) {
        try {

            ScheduleResult result = jpushClient.createSingleSchedule("scheduleMessage", time, pushPayload);
            return(result.getSchedule_id());
        } catch (APIConnectionException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        } catch (APIRequestException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        }
    }

    public void removeScheduleMessage(String jpushid) {
        try {
            jpushClient.deleteSchedule(jpushid);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        } catch (APIRequestException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        }
    }

   /* public void updateScheduleMessage(String jpushid,PushPayload pushPayload) {
        try {
            jpushClient.updateSchedule(jpushid,pushPayload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        } catch (APIRequestException e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SEND_MESSAGE_ERROR);
        }
    }
*/

}
